package com.lsj.controller;

import cn.hutool.core.convert.Convert;
import com.lsj.core.response.CommonResult;
import com.lsj.core.util.ExceptionHelperUtil;
import com.lsj.core.validate.Add;
import com.lsj.core.validate.Update;
import com.lsj.dto.sys.OrgAddDto;
import com.lsj.dto.sys.OrgSortDto;
import com.lsj.dto.sys.OrgUpdateDto;
import com.lsj.service.OrgService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.util.Map;

/**
 * @Author:lin.sj
 * @Data:2021/7/12 22:40
 * @Desc:
 */
@RestController
@RequestMapping("/sys_org")
@Api(tags = "组织机构处理")
public class OrgController {
    @Autowired
    OrgService orgService;

    @PostMapping("/add")
    @ApiOperation(value = "增加组织机构")
    public CommonResult addOrg(@RequestBody @Validated(Add.class) OrgAddDto dto){
        orgService.addOrg(dto);
        return  CommonResult.success();
    }

    @PostMapping("/delete")
    @ApiOperation(value = "删除组织机构")
    public CommonResult addOrg(@RequestBody Map param){
        Long orgId = Convert.toLong(param.get("orgId"));
        ExceptionHelperUtil.nullObjException(orgId,"菜单ID不能为空");
        orgService.deleteOrg(orgId);
        return  CommonResult.success();
    }

    @PostMapping("/updateSort")
    @ApiOperation(value = "更新菜单排序")
    public CommonResult updateMenuSort(@RequestBody @Validated(Update.class) OrgSortDto orgSortDto){
        orgService.updateOrgSort(orgSortDto);
        return  CommonResult.success();
    }

    @PostMapping("/update")
    @ApiOperation(value = "更新组织机构")
    public CommonResult updateOrg(@RequestBody @Validated(Update.class) OrgUpdateDto dto){
        orgService.updateOrg(dto);
        return  CommonResult.success();
    }

    @GetMapping("/get")
    @ApiOperation(value = "获取组织机构")
    public CommonResult getOrg(@NotNull(message = "主键不能为空") Long orgId){
        return CommonResult.success(orgService.getOrgById(orgId));
    }

    @GetMapping("/getPositiveOrgTree")
    @ApiOperation(value = "加载组织机构节点")
    public CommonResult getPositiveOrgTree(){
        return CommonResult.success(orgService.getPositiveOrgTree());
    }

}
